package com.mls.web.controller.api;

import com.mls.common.core.domain.AjaxResult;
import com.mls.common.utils.ShiroUtils;
import com.mls.education.domain.*;
import com.mls.education.service.*;
import com.mls.framework.jwt.utils.JwtUtils;
import com.mls.framework.shiro.service.SysPasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("api/global")
@ResponseBody
public class GlobalController {

    @Autowired
    IEduUserService eduUserService;

    @Autowired
    IEduCourseTypeService eduCourseTypeService;

    @Autowired
    IEduCourseService eduCourseService;

    @Autowired
    IEduCourseLessonService eduCourseLessonService;

    @Autowired
    IEduCommentService eduContentService;

    @Autowired
    SysPasswordService passwordService;
    private static final String  TYPE_USER = "用户";

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public AjaxResult login(EduUser eduUser){

        EduUser eduUserDB = eduUserService.selectUserByUsername(eduUser.getUsername());
        if(eduUserDB == null){
            return AjaxResult.error("登录失败，用户不存在");
        }
        //判断密码是否正确
        if (passwordService.matchesLoginUser(eduUserDB, eduUser.getPassword())) {
            //登录成功获取Token
            String token = JwtUtils.createToken(eduUserDB.getUsername(), eduUserDB.getPassword(), TYPE_USER);
            eduUserDB.setToken(token);
            return AjaxResult.success(eduUserDB);
        }
        return AjaxResult.error("密码不正确！");

    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public AjaxResult register(EduUser eduUser){
        EduUser eduUserDB = eduUserService.selectUserByUsername(eduUser.getUsername());
        if (eduUserDB != null){
            return AjaxResult.error("此用户名已被注册！");
        }
        //默认性别设置为'男'
        eduUser.setGender(0);
        eduUser.setSalt(ShiroUtils.randomSalt());
        eduUser.setPassword(passwordService.encryptPassword(eduUser.getUsername(), eduUser.getPassword(), eduUser.getSalt()));
        eduUserService.insertEduUser(eduUser);
        return AjaxResult.success(eduUser);
    }

    /**
     * 获取课程类别
     */
    @GetMapping("/coursetypes")
    public AjaxResult getCourseType(){
        List<EduCourseType> eduCourseTypes = eduCourseTypeService.selectEduCourseTypeList(new EduCourseType());
        return AjaxResult.success(eduCourseTypes);
    }

    /**
     * 获取课程信息
     */
    @GetMapping("/course/{courseId}")
    public AjaxResult getCourseById(@PathVariable Integer courseId){
        return AjaxResult.success(eduCourseService.selectEduCourseById(courseId));
    }

    /**
     * 获取课程列表
     */
    @GetMapping("/courses")
    public AjaxResult getQualityCourses(){
        List<EduCourse> eduCourses = eduCourseService.selectEduCourseList(new EduCourse());
        return AjaxResult.success(eduCourses);
    }

    /**
     * 根据课程ID获取播放列表
     */
    @GetMapping("/course/{courseId}/courselesson")
    public AjaxResult getCourseLesson(@PathVariable Integer courseId){
        EduCourseLesson eduCourseLesson = new EduCourseLesson();
        eduCourseLesson.setCourseId(courseId);
        List<EduCourseLesson> eduCourseLessons = eduCourseLessonService.selectEduCourseLessonList(eduCourseLesson);
        return AjaxResult.success(eduCourseLessons);
    }

    /**
     * 根据课时ID查询评论
     */
    @GetMapping("/courselesson/{lessonId}/comments")
    public AjaxResult getContentByLessonId(@PathVariable Integer lessonId){
        EduComment eduComment = new EduComment();
        eduComment.setLessonId(lessonId);
        List<EduComment> eduContents = eduContentService.selectEduCommentList(eduComment);
        AjaxResult result = AjaxResult.success(eduContents);
        //查询评论总数

        result.put("comment_num",eduContents.size());
        return result;
    }
}
